package interceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * 拦截器
 * 全都执行
 * @description:
 * @author: wangxun
 * @createDate: 2022/1/18
 * @version: 1.0
 */
public class Interceptor01 {
    /**
     * 拦截器
     */
    interface  WxInterceptor{
        /**
         * 拦截方法
         */
        Object plugin(Object target);
    }

    static class WxInterceptorA implements  WxInterceptor{

        @Override
        public Object plugin(Object target) {
            System.out.println("拦截器A");
            return target;
        }
    }
    static class WxInterceptorB implements  WxInterceptor{

        @Override
        public Object plugin(Object target) {
            System.out.println("拦截器B");
            return target;
        }
    }
    static class WxInterceptorC implements  WxInterceptor{

        @Override
        public Object plugin(Object target) {
            System.out.println("拦截器C");
            return target;
        }
    }

    static class WxInterceptorChain{
        private List<WxInterceptor> interceptors = new ArrayList<>();
        // 添加方法
        public void addInterceptor(WxInterceptor wxInterceptor){
            interceptors.add(wxInterceptor);
        }

        public void exeAll(Object target){
            interceptors.stream().forEach(wxInterceptor -> wxInterceptor.plugin(target));
        }


    }

    public static void main(String[] args) {
        WxInterceptorChain wxInterceptorChain = new WxInterceptorChain();
        wxInterceptorChain.addInterceptor(new WxInterceptorA());
        wxInterceptorChain.addInterceptor(new WxInterceptorB());
        wxInterceptorChain.addInterceptor(new WxInterceptorC());

        wxInterceptorChain.exeAll(new Object());

    }


}
